import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"

const Layouts = () => import("@/layouts/index.vue")

/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    // meta:{
    //   isStructure:true
    // },
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      },
      {
        path: "/routerclass",
        component: () => import("@/views/routerclass/index.vue"),
        name: "routerclass",
        meta: {
          title: "路由管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/sysRole",
        component: () => import("@/views/sysRole/index.vue"),
        name: "sysRole",
        meta: {
          title: "角色管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/user",
        component: () => import("@/views/user/index.vue"),
        name: "user",
        meta: {
          title: "用户管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/meeting",
        component: () => import("@/views/meeting/index.vue"),
        name: "meeting",
        meta: {
          title: "会议管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/schedule",
        component: () => import("@/views/schedule/index.vue"),
        name: "schedule",
        meta: {
          title: "进度计划管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/returnwork",
        component: () => import("@/views/returnwork/index.vue"),
        name: "returnwork",
        meta: {
          title: "开停复工令管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/engineeringcost",
        component: () => import("@/views/engineeringcost/index.vue"),
        name: "engineeringcost",
        meta: {
          title: "工程造价管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/engineeringimg",
        component: () => import("@/views/engineeringimg/index.vue"),
        name: "engineeringimg",
        meta: {
          title: "工程形象及影像记录",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/hidedanger",
        component: () => import("@/views/hidedanger/index.vue"),
        name: "hidedanger",
        meta: {
          title: "安全隐患管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/dangda",
        component: () => import("@/views/dangda/index.vue"),
        name: "dangda",
        meta: {
          title: "危大作业管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/safetyeduc",
        component: () => import("@/views/safetyeduc/index.vue"),
        name: "safetyeduc",
        meta: {
          title: "安全保密教育管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/compitems",
        component: () => import("@/views/compitems/index.vue"),
        name: "compitems",
        meta: {
          title: "已办事项",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/todolist",
        component: () => import("@/views/todolist/index.vue"),
        name: "todolist",
        meta: {
          title: "待办事项",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/leaveapp",
        component: () => import("@/views/leaveapp/index.vue"),
        name: "leaveapp",
        meta: {
          title: "员工请假单审批",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/leavepost",
        component: () => import("@/views/leavepost/index.vue"),
        name: "leavepost",
        meta: {
          title: "监理人员离岗工作交接单审批",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/device",
        component: () => import("@/views/device/index.vue"),
        name: "device",
        meta: {
          title: "设备及材料验收管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/sidestation",
        component: () => import("@/views/sidestation/index.vue"),
        name: "sidestation",
        meta: {
          title: "旁站管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/construction",
        component: () => import("@/views/construction/index.vue"),
        name: "construction",
        meta: {
          title: "施工方案管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/supervisionlog",
        component: () => import("@/views/supervisionlog/index.vue"),
        name: "supervisionlog",
        meta: {
          title: "监理日志管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/qualityissuetracking",
        component: () => import("@/views/qualityissuetracking/index.vue"),
        name: "qualityissuetracking",
        meta: {
          title: "质量问题跟踪",
          svgIcon: "dashboard"
        }
      },

      {
        path: "/openletter",
        component: () => import("@/views/letters/openletter/index.vue"),
        name: "openletter",
        meta: {
          title: "发函输入",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/letterapproval",
        component: () => import("@/views/letters/letterapproval/index.vue"),
        name: "letterapproval",
        meta: {
          title: "发函审批",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/letterchannle",
        component: () => import("@/views/letters/letterchannle/index.vue"),
        name: "letterchannle",
        meta: {
          title: "发函渠道号管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/letteracceptance",
        component: () => import("@/views/letters/letteracceptance/index.vue"),
        name: "letteracceptance",
        meta: {
          title: "收函接收/著录/导入",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/letterdistribution",
        component: () => import("@/views/letters/letterdistribution/index.vue"),
        name: "letterdistribution",
        meta: {
          title: "内部分发已接收函件",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/meeting/ledger",
        component: () => import("@/views/meeting/ledger/index.vue"),
        name: "meeting_ledger",
        meta: {
          title: "会议行动台账管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/meeting/summary",
        component: () => import("@/views/meeting/summary/index.vue"),
        name: "meeting_summary",
        meta: {
          title: "会议纪要管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/schedule/tracking",
        component: () => import("@/views/schedule/tracking/index.vue"),
        name: "schedule_tracking",
        meta: {
          title: "进度跟踪对比信息管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/schedule/node",
        component: () => import("@/views/schedule/node/index.vue"),
        name: "schedule_node",
        meta: {
          title: "工程进度节点信息",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/returnwork/start",
        component: () => import("@/views/returnwork/start/index.vue"),
        name: "returnwork_start",
        meta: {
          title: "工程开工令管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/returnwork/suspend",
        component: () => import("@/views/returnwork/suspend/index.vue"),
        name: "returnwork_suspend",
        meta: {
          title: "工程暂停令管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/returnwork/return",
        component: () => import("@/views/returnwork/return/index.vue"),
        name: "returnwork_return",
        meta: {
          title: "工程复工令管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/engineeringcost/quantity",
        component: () => import("@/views/engineeringcost/quantity/index.vue"),
        name: "engineeringcost_quantity",
        meta: {
          title: "工程量计量文件管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/engineeringcost/visa",
        component: () => import("@/views/engineeringcost/visa/index.vue"),
        name: "engineeringcost_visa",
        meta: {
          title: "现场签证及见证记录管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/procedure/fileinput",
        component: () => import("@/views/procedure/fileinput/index.vue"),
        name: "procedure_fileinput",
        meta: {
          title: "程序文件输入",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/procedure/review",
        component: () => import("@/views/procedure/review/index.vue"),
        name: "procedure_review",
        meta: {
          title: "程序文件审查流程",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/procedure/effective",
        component: () => import("@/views/procedure/effective/index.vue"),
        name: "procedure_effective",
        meta: {
          title: "程序文件生效发布流程",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/experience/enter",
        component: () => import("@/views/experience/enter/index.vue"),
        name: "experience_enter",
        meta: {
          title: "经验反馈归档录入",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/experience/distribute",
        component: () => import("@/views/experience/distribute/index.vue"),
        name: "experience_distribute",
        meta: {
          title: "经验反馈分发传阅",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/designfile/enter",
        component: () => import("@/views/designfile/enter/index.vue"),
        name: "designfile_enter",
        meta: {
          title: "设计文件接收/著录/导入",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/designfile/distribute",
        component: () => import("@/views/designfile/distribute/index.vue"),
        name: "designfile_distribute",
        meta: {
          title: "设计文件分发",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/designfile/flowpath",
        component: () => import("@/views/designfile/flowpath/index.vue"),
        name: "designfile_flowpath",
        meta: {
          title: "设计文件内部分发流程",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/work/enter",
        component: () => import("@/views/work/enter/index.vue"),
        name: "work_enter",
        meta: {
          title: "创建联系单/通知单",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/work/examine",
        component: () => import("@/views/work/examine/index.vue"),
        name: "work_examine",
        meta: {
          title: "联系单/通知单审批",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/work/result",
        component: () => import("@/views/work/result/index.vue"),
        name: "work_result",
        meta: {
          title: "联系单/通知单结果录入",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/standards/piping",
        component: () => import("@/views/standards/piping/index.vue"),
        name: "standards_piping",
        meta: {
          title: "管道标准",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/standards/HVAC",
        component: () => import("@/views/standards/HVAC/index.vue"),
        name: "standards_HVAC",
        meta: {
          title: "暖通标准",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/standards/electrical",
        component: () => import("@/views/standards/electrical/index.vue"),
        name: "standards_electrical",
        meta: {
          title: "电气标准",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/standards/device",
        component: () => import("@/views/standards/device/index.vue"),
        name: "standards_device",
        meta: {
          title: "设备标准",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/standards/engineering",
        component: () => import("@/views/standards/engineering/index.vue"),
        name: "standards_engineering",
        meta: {
          title: "土建标准",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/standards/other",
        component: () => import("@/views/standards/other/index.vue"),
        name: "standards_other",
        meta: {
          title: "其他标准",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/filebox",
        component: () => import("@/views/filebox/index.vue"),
        name: "filebox",
        meta: {
          hidden: true,
          title: "查看文件",
          svgIcon: "dashboard"
        }
      },

      {
        path: "/tableDetails",
        component: () => import("@/views/tableDetails/index.vue"),
        name: "tableDetails",
        meta: {
          hidden: true,
          title: "详情",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/examination",
        component: () => import("@/views/examination/index.vue"),
        name: "examination",
        meta: {
          hidden: true,
          title: "考题",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/examinationModle",
        component: () => import("@/views/examinationModle/index.vue"),
        name: "examinationModle",
        meta: {
          title: "试卷模块管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/examinationModle_details",
        component: () => import("@/views/examinationModle/details.vue"),
        name: "examinationModle_details",
        meta: {
          title: "试卷详情管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/examinationModle_log",
        component: () => import("@/views/examinationModle/log.vue"),
        name: "examinationModle_log",
        meta: {
          title: "考试记录管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/examinationModle_treatlog",
        component: () => import("@/views/examinationModle/treatlog.vue"),
        name: "examinationModle_treatlog",
        meta: {
          title: "待考试管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/appeal",
        component: () => import("@/views/appeal/index.vue"),
        name: "appeal",
        meta: {
          title: "申诉管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/violating",
        component: () => import("@/views/violating/index.vue"),
        name: "violating",
        meta: {
          title: "违规行为举报",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/punish",
        component: () => import("@/views/punish/index.vue"),
        name: "punish",
        meta: {
          title: "员工违规处罚管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/punish_user",
        component: () => import("@/views/punish/user.vue"),
        name: "punish_user",
        meta: {
          title: "员工违规处罚管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/learning",
        component: () => import("@/views/learning/index.vue"),
        name: "learning",
        meta: {
          title: "学习管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/abnormal",
        component: () => import("@/views/abnormal/index.vue"),
        name: "abnormal",
        meta: {
          title: "异常转正常记录",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/abnormal_handle",
        component: () => import("@/views/abnormal/handle.vue"),
        name: "abnormal_handle",
        meta: {
          title: "异常状态员工的转正常申请",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/appeal_dai",
        component: () => import("@/views/appeal/dai.vue"),
        name: "appeal_dai",
        meta: {
          title: "代员工状态申述",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/studytraining",
        component: () => import("@/views/studytraining/index.vue"),
        name: "studytraining",
        meta: {
          title: "员工学习培训记录",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/setRPRules",
        component: () => import("@/views/setRPRules/index.vue"),
        name: "setRPRules",
        meta: {
          title: "设置奖罚分的规则",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/EAndExitRules",
        component: () => import("@/views/EAndExitRules/index.vue"),
        name: "EAndExitRules",
        meta: {
          title: "出入场的规则管理",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/abnormalUser",
        component: () => import("@/views/abnormalUser/index.vue"),
        name: "abnormalUser",
        meta: {
          title: "异常员工的状态",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/exitUser",
        component: () => import("@/views/exitUser/index.vue"),
        name: "exitUser",
        meta: {
          title: "员工退场登记",
          svgIcon: "dashboard"
        }
      },
      {
        path: "/showUser",
        component: () => import("@/views/showUser/index.vue"),
        name: "showUser",
        meta: {
          title: "查看员工信息",
          svgIcon: "dashboard"
        }
      }
    ]
  },
  // {
  //   path: "/unocss",
  //   component: Layouts,
  //   redirect: "/unocss/index",
  //   children: [
  //     {
  //       path: "index",
  //       component: () => import("@/views/unocss/index.vue"),
  //       name: "UnoCSS",
  //       meta: {
  //         title: "unocss",
  //         svgIcon: "unocss"
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/link",
  //   component: Layouts,
  //   children: [
  //     {
  //       path: "https://juejin.cn/post/7089377403717287972",
  //       component: () => {},
  //       name: "Link",
  //       meta: {
  //         title: "外链",
  //         svgIcon: "link"
  //       }
  //     }
  //   ]
  // },
  {
    path: "/table",
    component: Layouts,
    redirect: "/table/element-plus",
    name: "Table",
    meta: {
      title: "表格",
      elIcon: "Grid"
    },
    children: [
      {
        path: "element-plus",
        component: () => import("@/views/table/element-plus/index.vue"),
        name: "ElementPlus",
        meta: {
          title: "Element Plus",
          keepAlive: true
        }
      },
      {
        path: "vxe-table",
        component: () => import("@/views/table/vxe-table/index.vue"),
        name: "VxeTable",
        meta: {
          title: "Vxe Table",
          keepAlive: true
        }
      }
    ]
  }
  // {
  //   path: "/menu",
  //   component: Layouts,
  //   redirect: "/menu/menu1",
  //   name: "Menu",
  //   meta: {
  //     title: "多级路由",
  //     svgIcon: "menu"
  //   },
  //   children: [
  //     {
  //       path: "menu1",
  //       component: () => import("@/views/menu/menu1/index.vue"),
  //       redirect: "/menu/menu1/menu1-1",
  //       name: "Menu1",
  //       meta: {
  //         title: "menu1"
  //       },
  //       children: [
  //         {
  //           path: "menu1-1",
  //           component: () => import("@/views/menu/menu1/menu1-1/index.vue"),
  //           name: "Menu1-1",
  //           meta: {
  //             title: "menu1-1",
  //             keepAlive: true
  //           }
  //         },
  //         {
  //           path: "menu1-2",
  //           component: () => import("@/views/menu/menu1/menu1-2/index.vue"),
  //           redirect: "/menu/menu1/menu1-2/menu1-2-1",
  //           name: "Menu1-2",
  //           meta: {
  //             title: "menu1-2"
  //           },
  //           children: [
  //             {
  //               path: "menu1-2-1",
  //               component: () => import("@/views/menu/menu1/menu1-2/menu1-2-1/index.vue"),
  //               name: "Menu1-2-1",
  //               meta: {
  //                 title: "menu1-2-1",
  //                 keepAlive: true
  //               }
  //             },
  //             {
  //               path: "menu1-2-2",
  //               component: () => import("@/views/menu/menu1/menu1-2/menu1-2-2/index.vue"),
  //               name: "Menu1-2-2",
  //               meta: {
  //                 title: "menu1-2-2",
  //                 keepAlive: true
  //               }
  //             }
  //           ]
  //         },
  //         {
  //           path: "menu1-3",
  //           component: () => import("@/views/menu/menu1/menu1-3/index.vue"),
  //           name: "Menu1-3",
  //           meta: {
  //             title: "menu1-3",
  //             keepAlive: true
  //           }
  //         }
  //       ]
  //     },
  //     {
  //       path: "menu2",
  //       component: () => import("@/views/menu/menu2/index.vue"),
  //       name: "Menu2",
  //       meta: {
  //         title: "menu2",
  //         keepAlive: true
  //       }
  //     }
  //   ]
  // },
  // {
  //   path: "/hook-demo",
  //   component: Layouts,
  //   redirect: "/hook-demo/use-fetch-select",
  //   name: "HookDemo",
  //   meta: {
  //     title: "hook 示例",
  //     elIcon: "Menu",
  //     alwaysShow: true
  //   },
  //   children: [
  //     {
  //       path: "use-fetch-select",
  //       component: () => import("@/views/hook-demo/use-fetch-select.vue"),
  //       name: "UseFetchSelect",
  //       meta: {
  //         title: "useFetchSelect"
  //       }
  //     },
  //     {
  //       path: "use-fullscreen-loading",
  //       component: () => import("@/views/hook-demo/use-fullscreen-loading.vue"),
  //       name: "UseFullscreenLoading",
  //       meta: {
  //         title: "useFullscreenLoading"
  //       }
  //     }
  //   ]
  // }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/permission",
    component: Layouts,
    redirect: "/permission/page",
    name: "Permission",
    meta: {
      title: "权限管理",
      svgIcon: "lock",
      roles: ["admin", "editor"], // 可以在根路由中设置角色
      alwaysShow: true // 将始终显示根菜单
    },
    children: [
      {
        path: "page",
        component: () => import("@/views/permission/page.vue"),
        name: "PagePermission",
        meta: {
          title: "页面权限",
          roles: ["admin"] // 或者在子导航中设置角色
        }
      },
      {
        path: "directive",
        component: () => import("@/views/permission/directive.vue"),
        name: "DirectivePermission",
        meta: {
          title: "指令权限" // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  },
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
